//
//  BKGDManagerTitleView.m
//  bestkeep
//
//  Created by 毛韶谦 on 2016/11/11.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKGDManagerTitleView.h"

#define BKGDMainTitleViewItemSelectedColor [UIColor blackColor]
#define BKGDMainTitleViewItemDeSelectedColor [UIColor grayColor]
#define BKGDMainTitleViewItemSelectedFont [UIFont systemFontOfSize:18]
#define BKGDMainTitleViewItemDeSelectedFont [UIFont systemFontOfSize:17]

@interface BKGDManagerTitleViewCell : UICollectionViewCell

@property(nonatomic,strong) UILabel *titleLabel;

@end

@implementation BKGDManagerTitleViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self initialize];
    }
    
    return self;
}

-(void) initialize {
    
    _titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
    _titleLabel.font = BKGDMainTitleViewItemDeSelectedFont;
    _titleLabel.textColor = BKGDMainTitleViewItemDeSelectedColor;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    
    [self.contentView addSubview:_titleLabel];
    
}

-(void)layoutSubviews {
    [super layoutSubviews];
    _titleLabel.frame = self.bounds;
}

-(void)setSelected:(BOOL)selected {
    if(selected) {
        _titleLabel.textColor = BKGDMainTitleViewItemSelectedColor;
        _titleLabel.font = BKGDMainTitleViewItemSelectedFont;
        
    } else {
        _titleLabel.textColor = BKGDMainTitleViewItemDeSelectedColor;
        _titleLabel.font = BKGDMainTitleViewItemDeSelectedFont;
    }
}

@end






#define BKGDManagerTitleViewItemHeight 40

NSString *const BKGDManagerTitleViewCellIdentifier = @"BKGDManagerTitleViewCellIdentifier";

@interface BKGDManagerTitleView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong) UICollectionView *collectionView;

@property(nonatomic,strong) UIView *selectedLineView;

@end



@implementation BKGDManagerTitleView

-(instancetype)initWithFrame:(CGRect)frame {
    frame = CGRectMake(0, 0, 150, BKGDManagerTitleViewItemHeight);
    self = [super initWithFrame:frame];
    if(self) {
        [self initialize];
    }
    return self;
}

-(void)initialize {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(50, BKGDManagerTitleViewItemHeight);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[BKGDManagerTitleViewCell class] forCellWithReuseIdentifier:BKGDManagerTitleViewCellIdentifier];
    [self addSubview:_collectionView];
    
    _selectedLineView = [[UIView alloc] initWithFrame:CGRectMake(7, BKGDManagerTitleViewItemHeight, layout.itemSize.width - 14, 2)];
    _selectedLineView.backgroundColor = [UIColor blackColor];
    _selectedLineView.hidden = YES;
    [self addSubview:_selectedLineView];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    _collectionView.frame = self.bounds;
}

-(void)setIsShowIndicator:(BOOL)isShowIndicator {
    _isShowIndicator = isShowIndicator;
    _selectedLineView.hidden = !_isShowIndicator;
}

-(void)setTitles:(NSArray<NSString *> *)titles {
    _titles = titles;
    
    [_collectionView reloadData];
    if(titles) {
        [_collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
    }
}

-(void)setCurrentIndex:(NSInteger)currentIndex {
    
    _currentIndex = currentIndex;
    
    if(_titles && currentIndex >= 0 && currentIndex < _titles.count) {
        [_collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:currentIndex inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
        
        [self updateSelectedLineLayout];
    }
}

-(void)updateSelectedLineLayout {
    UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0]];
    CGRect rect = [self convertRect:cell.frame toView:self];
    [UIView animateWithDuration:0.3 animations:^{
        _selectedLineView.frame = CGRectMake(CGRectGetMinX(rect) + 7, CGRectGetMaxY(rect), CGRectGetWidth(rect) - 14, 2);
    }];
}

#pragma -mark collectionView delegate

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(!_titles) {
        return 0;
    }
    return _titles.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BKGDManagerTitleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BKGDManagerTitleViewCellIdentifier forIndexPath:indexPath];
    
    cell.titleLabel.text = _titles[indexPath.row];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _currentIndex = indexPath.row;
    
    [self updateSelectedLineLayout];
    
    if(_delegate && [_delegate respondsToSelector:@selector(titleView:didSelectedAtIndex:)]) {
        [_delegate titleView:self didSelectedAtIndex:indexPath.row];
    }
}

@end
